// 价格过滤器，保留两位小数，type传0为整数；
const moneyFormat = (num) => {
  var result = parseFloat(num);
  if (isNaN(result)) {
    return 0.00;
  }
  result = Math.round(num * 100) / 100;
  var s_x = result.toString();
  var pos_decimal = s_x.indexOf('.');
  if (pos_decimal < 0) {
    pos_decimal = s_x.length;
    s_x += '.';
  }
  while (s_x.length <= pos_decimal + 2) {
    s_x += '0';
  }
  return s_x;
}

const dateFormat = (dates,fmt)=> {
	if(dates==""){return ""}
  if(!fmt){fmt = "yyyy-mm-dd HH:MM:SS" +
    ""}
	let ret;
	let date = new Date(dates);
    const opt = {
        "y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}


export {
  moneyFormat,
  dateFormat
}


//计算年龄
export function dealAge(str) {
  let age = "";
  if (str) {
    let list = str.split(",")

    if (list.length > 2) {
      if (list[0] > 0) {
        age = list[0] + "岁"
      } else {
        if (list[1] > 0) {
          age = list[1] + "月" + list[2] + "天"
        } else {
          age = list[2] + "天"
        }
      }
    } else {
      age = str;
    }
  }

  return age
}
